package com.polaris.live.dialog

import android.content.Context
import android.text.Spannable
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.view.View
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.StringUtils
import com.bumptech.glide.Glide
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.databinding.DialogOpenVipBinding
import com.polaris.live.manager.LiveStateManager
import com.polaris.live.translation.R.string
import com.polaris.live.ui.party.PartyUserStateUtil
import com.polaris.live.utils.route.RoutePath
import com.polaris.live.utils.route.Router

/**
 * 开通vip
 *
 * @author Created by 半仙 on 2024/12/21/021 11:51
 */
class OpenVipDialog(
    context: Context,
    private val mTvDetail: String,
    private val mVipLevel: Int? = null,
    private val mImageUrl: String? = null,
    private val mHightTips: String?,
    private val showGoVip: Boolean, //显示成为ViP
) : CenterPopupView(context) {

    override fun getImplLayoutId() = R.layout.dialog_open_vip

    private lateinit var mBinding: DialogOpenVipBinding

    override fun onCreate() {
        super.onCreate()
        mBinding = DialogOpenVipBinding.bind(popupImplView)

        mBinding.apply {
            if (!showGoVip) {
                tvConfirm.text = StringUtils.getString(string.ok)
            }
            ivClose.setOnSingleClickListener {
                dismiss()
            }
            tvDetail.text = mTvDetail
            tvConfirm.setOnSingleClickListener {
                if (showGoVip) {
                    if (PartyUserStateUtil.inTheGame) {
                        ToastGravity.showGravity(string.you_play_game_dont_leave)
                        return@setOnSingleClickListener
                    }
                    Router.builder(RoutePath.VIP_CENTER_ACTIVITY)
                        .withInt("page", 0)
                        .withInt("level", mVipLevel ?: 6)
                        .navigation(context)
                } else {
                    dismiss()
                }

            }

            if (LiveStateManager.isLiving()) {
                tvConfirm.visibility = View.GONE
            }

            if (!mImageUrl.isNullOrEmpty()) {
                ivContent.visibility = View.VISIBLE
                Glide.with(context).load(mImageUrl).into(ivContent)
            }
            if (!mHightTips.isNullOrEmpty()) {
                val spannable = SpannableString(mTvDetail)
                // 找到 hightTips 在原文本中的起始和结束位置
                val startIndex = mTvDetail.indexOf(mHightTips)
                val endIndex = startIndex + mHightTips.length
                // 设置颜色
                spannable.setSpan(
                    ForegroundColorSpan(ColorUtils.getColor(R.color.color_ffffa100)),
                    startIndex,
                    endIndex,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
                )
                tvDetail.text = spannable
            }
        }
    }

    companion object {

        fun show(
            context: Context,
            tvDetail: String,
            vipLevel: Int? = null,
            imageUrl: String? = null,
            hightTips: String? = null,
            showGoVip: Boolean = true,
        ) {
            XPopup.Builder(context)
                .isDestroyOnDismiss(true)
                .asCustom(OpenVipDialog(context, tvDetail, vipLevel, imageUrl, hightTips, showGoVip))
                .show()
        }
    }
}